package top.api.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import top.api.common.ResponseResult;
import top.api.domain.dto.RoleAddDto;
import top.api.domain.dto.RoleUpdateDto;
import top.api.domain.dto.RoleUpdateStatusDto;
import top.api.service.RoleService;

@RestController
@RequestMapping("/system/role")
@Api(tags = "角色管理")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 分页查询角色
     * @param pageNum
     * @param pageSize
     * @param roleName 角色名称
     * @param status 状态
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询角色")
    @PreAuthorize("@ps.hasPermission('system:role:query')") // 查询角色 权限校验
    public ResponseResult list(@RequestParam Integer pageNum, @RequestParam Integer pageSize,String roleName, String status){
        return roleService.getRoleListPage(pageNum,pageSize,roleName,status);
    }

    /**
     * 修改角色状态
     * @param roleUpdateStatusDto
     * @return
     */
    @PutMapping("/changeStatus")
    @ApiOperation(value = "修改角色状态")
    @PreAuthorize("@ps.hasPermission('system:role:edit')") // 修改角色 权限校验
    public ResponseResult updateStatus(@RequestBody RoleUpdateStatusDto roleUpdateStatusDto){
        return roleService.updateStatus(roleUpdateStatusDto);
    }

    /**
     * 添加角色
     * @param roleAddDto
     * @return
     */
    @PostMapping
    @ApiOperation(value = "添加角色")
    @PreAuthorize("@ps.hasPermission('system:role:add')") // 添加角色 权限校验
    public ResponseResult addRole(@RequestBody RoleAddDto roleAddDto){
        return roleService.addRole(roleAddDto);
    }

    /**
     * 查询角色信息详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询角色详情")
    @PreAuthorize("@ps.hasPermission('system:role:query')") // 查询角色 权限校验
    public ResponseResult getRoleInfo(@PathVariable Long id){
        return roleService.getRoleInfo(id);
    }


    /**
     * 修改角色
     * @param roleUpdateDto
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改角色")
    @PreAuthorize("@ps.hasPermission('system:role:edit')") // 修改角色 权限校验
    public ResponseResult updateRole(@RequestBody RoleUpdateDto roleUpdateDto){
        return roleService.updateRole(roleUpdateDto);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除角色")
    @PreAuthorize("@ps.hasPermission('system:role:remove')") // 删除角色 权限校验
    public ResponseResult remoteRole(@PathVariable Long id){
        return roleService.removeRole(id);
    }


    /**
     * 查询所有角色 (正常角色)
     * @return
     */
    @GetMapping("/listAllRole")
    @ApiOperation(value = "分页查询角色")
    @PreAuthorize("@ps.hasPermission('system:role:query')") // 查询角色 权限校验
    public ResponseResult listAllRole(){
        return roleService.getListAllRole();
    }
}
